#include "non_status_control/non_status_control.hpp"

#ifdef DBG_TAG
#undef DBG_TAG
#endif
#ifdef DBG_LVL
#undef DBG_LVL
#endif

#define DBG_TAG "nonstatus_ctl"
#define DBG_LVL DBG_LOG

extern "C"
{
#include <rtdbg.h>
}

/******************************************************************************
 *
 * @brief:    系统状态及提示灯处理
 *
 ******************************************************************************/
void state_thread_entry(void *pra)
{
    state_status flag;
    double temp;
    while (1)
    {
        rt_thread_mdelay(2000);
        flag = system_mechine->STATE_get_system_status();
        switch (flag)
        {
        case state_status_runing:
            sys_led->BEEP_LED_send_beep(0, 1000, 1000);
            break;
        case state_status_device_offline:
            LOG_E("sys device offline,pls contact tech");
            sys_led->BEEP_LED_send_beep(0, 300, 300);

            if (flow_device->PWM3901_read_id() == pmw3901_status_ok)
                if (ultra_sonic->DISTANCE_SONIC_read_distance(&temp) == distance_status_ok)
                {
                    rt_thread_mdelay(3000); //等待启动完成

                    if (flow_device->PMW3901_init() == pmw3901_status_ok)
                    {
                        system_mechine->STATE_set_system_status(state_status_runing);
                        LOG_I("sys device online");
                    }
                }

            break;
        default:
            break;
        }
    }
}

/******************************************************************************
 *
 * @brief:    数据储存轮训
 *
 ******************************************************************************/
void record_data_thread_entry(void *pra)
{
    float input_voltage;

    global_data->DATA_SAVE_read_short_cycle_data("IN_V", &input_voltage);
    LOG_D("last input voltage is %f", input_voltage);
//    while (1)
//    {
//
////        input_voltage = voltage_device->CHIP_ADC_measure();
////        LOG_D("input voltage is %f",input_voltage);
//
////        global_data->DATA_SAVE_write_short_cycle_data("IN_V", input_voltage);
////        global_data->DATA_SAVE_record_short_cycle_data();
//        rt_thread_mdelay(1000);
//    }
}

/******************************************************************************
 *
 * @brief:    光流轮训
 *
 ******************************************************************************/
void flow_keep_alive(void*pra)
{
    state_status flag;
    while (1)
    {
        rt_thread_mdelay(5000);
        flag = system_mechine->STATE_get_system_status();
        if (flag != state_status_runing)
            continue;

        if (flow_device->PWM3901_read_id() != pmw3901_status_ok)
            system_mechine->STATE_set_system_status(state_status_device_offline);

    }
}

//#define FLOW_WAVE_TEST
void flow_add_wave_thread_entry(void *pra)
{
#ifdef FLOW_WAVE_TEST
    uint32_t all_wave = 0;
    uint8_t change_flag = 0;
#endif
    uint32_t temp;
    while (1)
    {
        rt_thread_mdelay(10);
        if (flow_device->PWM3901_read_out_shift_and_reduce(&temp) == pmw3901_status_ok)
        {
#ifdef FLOW_WAVE_TEST
            if(all_wave != all_wave+temp)
            change_flag = 1;
            all_wave += temp;
            LOG_D("send wave num <%u>",temp);
#endif
            sim_sig->SIMULATE_SIGNAL_add_signal(temp);
        }
#ifdef FLOW_WAVE_TEST
        if(change_flag)
        {
            LOG_D("all send wave num <%u>",all_wave);
            change_flag = 0;
        }
#endif

    }
}
//#define ULTRA_SONIC_DEBUG
#define ULTRA_SONIC_DETECT_CYCLE_TIMES 200
//光流-超声波计算位移
void flow_thread_entry(void *pra)
{
    uint32_t sonic_detect_times = 0;
    double sonic_height = 0;
    state_status flag;
    while (1)
    {
        rt_thread_mdelay(10);
        if (sonic_detect_times % ULTRA_SONIC_DETECT_CYCLE_TIMES == 0)
        {
            if (ultra_sonic->DISTANCE_SONIC_read_distance(&sonic_height) != distance_status_ok)
#ifdef ULTRA_SONIC_DEBUG
                {
#endif
                system_mechine->STATE_set_system_status(state_status_device_offline);

#ifdef ULTRA_SONIC_DEBUG
            LOG_W("sonic offline");
        }
#endif

#ifdef ULTRA_SONIC_DEBUG
            else
            LOG_D("sonic height is <%.4f>mm", sonic_height);
#endif
            sonic_detect_times = 1;
        }
        else
        {
            sonic_detect_times++;
        }

        flag = system_mechine->STATE_get_system_status();
        if (flag != state_status_runing)
        {

            flow_device->PWM3901_count_shift_with_height(0);
            LOG_W("state offline set height to zero");
        }
        else
        {
            flow_device->PWM3901_count_shift_with_height(sonic_height);
        }
    }
}

/******************************************************************************
 *
 * @brief:    补光板
 *
 ******************************************************************************/
void led_board_thread_entry(void *pra)
{

    led_board_pwm->CHIP_PWM_close();
    fan_pwm->CHIP_PWM_close();

    led_board_pwm->CHIP_PWM_open();
    fan_pwm->CHIP_PWM_open();

    led_board_pwm->CHIP_PWM_change_duty_ratio(50);
    fan_pwm->CHIP_PWM_change_duty_ratio(50);

}

/******************************************************************************
 *
 * @brief:     轮速传感器
 *
 ******************************************************************************/

void sim_sig_thread_entry(void *pra)
{
    sim_sig->SIMULATE_SIGNAL_in_thread_function();
}

/******************************************************************************
 *
 * @brief:    开启所有无状态控制
 *
 ******************************************************************************/

void non_status_control_thread_start()
{
    rt_thread_t tid;

    tid = rt_thread_create("led_b", led_board_thread_entry, RT_NULL, 2048, 5, 20);
    if (tid)
        rt_thread_startup(tid);
    else
        LOG_E("led_b thread not start!");

    record_tid = rt_thread_create("record", record_data_thread_entry, RT_NULL, 2048, 16, 20);
    if (record_tid)
        rt_thread_startup(record_tid);
    else
        LOG_E("record thread not start!");

    tid = rt_thread_create("flow_wa", flow_add_wave_thread_entry, RT_NULL, 2048, 10, 100);
    if (tid)
        rt_thread_startup(tid);
    else
        LOG_E("flow_wa thread not start!");

    tid = rt_thread_create("flow", flow_thread_entry, RT_NULL, 2048, 10, 20);
    if (tid)
        rt_thread_startup(tid);
    else
        LOG_E("flow thread not start!");

    tid = rt_thread_create("flow_ka", flow_keep_alive, RT_NULL, 2048, 9, 20);
    if (tid)
        rt_thread_startup(tid);
    else
        LOG_E("flow_ka thread not start!");

//    tid = rt_thread_create("sonic", ultra_sonic_thread_entry, RT_NULL, 2048, 9, 20);
//    if (tid)
//        rt_thread_startup(tid);
//    else
//        LOG_E("sonic thread not start!");

    tid = rt_thread_create("sim_sig", sim_sig_thread_entry, RT_NULL, 2048, 10, 500);
    if (tid)
        rt_thread_startup(tid);
    else
        LOG_E("sim_sig thread not start!");

    tid = rt_thread_create("state", state_thread_entry, RT_NULL, 2048, 12, 20);
    if (tid)
        rt_thread_startup(tid);
    else
        LOG_E("sim_sig thread not start!");

}

